package com.shuangyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuangyu.common.R;
import com.shuangyu.dto.DishDto;
import com.shuangyu.entity.Category;
import com.shuangyu.entity.Dish;
import com.shuangyu.entity.DishFlavor;
import com.shuangyu.service.CategoryService;
import com.shuangyu.service.DishFlavorService;
import com.shuangyu.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> page(int page, int pageSize, String name){
        //创建菜品的分页查询对象
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        //创建DishDto的分页对象
        Page<DishDto> dishDtoPage = new Page<>();

        //菜品查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Dish::getName, name).orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        //查询菜品数据
        dishService.page(pageInfo, queryWrapper);
        //把除了菜品信息的数据copy到dishDtoPage
        BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");

        //根据每条菜品的分类id查询分类名，然后把菜品信息和分类名封装到dishDto集合
        List<DishDto> list = pageInfo.getRecords().stream().map((item) -> {
            DishDto dishDto = new DishDto();

            Long categoryId = item.getCategoryId();
            String categoryName = categoryService.getById(categoryId).getName();

            BeanUtils.copyProperties(item, dishDto);
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());

        //把菜品的信息集合赋值给dishDto分页对象
        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);
    }

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        //设置随机商品码
        String s = UUID.randomUUID().toString();
        dishDto.setCode(s);

        dishService.saveWithFlavor(dishDto);

        return R.success("新增成功");
    }

    /**
     * 删除菜品及对应的口味
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){

        dishService.removeWithFlavor(ids);

        return R.success("删除成功");
    }

    /**
     * 停售菜品
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> status0(String ids){

        String[] idss = ids.split(",");

        for(String id : idss){
            LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(Dish::getStatus, 0).eq(Dish::getId, id);

            dishService.update(updateWrapper);
        }



        return R.success("操作成功");
    }

    /**
     * 启售菜品
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> status1(String ids){

        String[] idss = ids.split(",");

        for(String id : idss){
            LambdaUpdateWrapper<Dish> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(Dish::getStatus, 1).eq(Dish::getId, id);

            dishService.update(updateWrapper);
        }

        return R.success("操作成功");
    }

    /**
     * 根据id查询菜品的信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable String id){
        DishDto dishDto = dishService.getWithFlavorById(id);
        return R.success(dishDto);
    }

    /**
     * 更新菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){

        dishService.updateWithFlavorById(dishDto);

        return R.success("操作成功");
    }

    /**
     * 查询所有菜品及对应口味
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(String categoryId){


        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(categoryId != null, Dish::getCategoryId, categoryId);
        queryWrapper.eq(Dish::getStatus, 1);
        queryWrapper.orderByAsc(Dish::getSort);
        List<Dish> dishList = dishService.list(queryWrapper);

        List<DishDto> dishDtoList = dishList.stream().map((item) -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item, dishDto);

            LambdaQueryWrapper<DishFlavor> flavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            flavorLambdaQueryWrapper.eq(DishFlavor::getDishId, item.getId());
            List<DishFlavor> flavors = dishFlavorService.list(flavorLambdaQueryWrapper);

            dishDto.setFlavors(flavors);

            return dishDto;
        }).collect(Collectors.toList());


        return R.success(dishDtoList);
    }

}
